import {config} from "../../../config.js";
import { UserModel} from "../../../models/user.js";
import Dialog from '../../../miniprogram_npm/vant-weapp/dialog/dialog';
var _UserModel = new UserModel();
var _self;
Page({
  data: {
      messages:[],
      show:false,
      currentMsg:{},
      currentTarget:0,
      reject_message:"",
      pagenation: {
        pageNum: 1,  //页码
        pageSize: config.pageSize, //每页显示多少数据
        pages: 0,   //一共有几页
        total: 0,
        hasMore: true //是否有更多数据 
      }
  },
  reject_messageChange:function(e){
    _self.setData({
      reject_message:e.detail.value,
      errorMsg: ""
    })
  },
  onLoad: function (options) {
    _self = this;
    this.getUserMessage(_self.data.pagenation)
  },
  getUserMessage(pagenation){
    _UserModel.getUsrMessageWithPagenation(pagenation, function (data) {
      if (data.length > 0) {
        console.log(data[0])
        if (data[0].list.length < _self.data.pagenation.pageSize) {
          _self.setData({
            ['pagenation.hasMore']: false,
            isNoMoreData: true
          })
        }
        _self.setData({
          ['pagenation.pageNum']: data[0].pageNum,
          ['pagenation.pageSize']: data[0].pageSize,
          ['pagenation.pages']: data[0].pages,
          ['pagenation.total']: data[0].total,
          messages: _self.data.messages.concat(data[0].list)
        });
      }
    })
  },
  onClose:function(e){
    if (_self.data.reject_message == ''){
          _self.setData({
            errorMsg:"请输入拒绝原因"
          })
          return;
        }
        let message = _self.data.currentMsg;
        let uid =message.messageSource;
        let params={
          "handleUserId": uid,
            "ischecked": "03" //03不通过
        } 
    _UserModel.handleAuthentication(params, function (data) { 
          let messageId = message.messageId;
          let messageArr = new Array(messageId + "");
          let params = {
            "messageIdList": messageArr,
            "messageStatus": "4",
            "remark":_self.data.message
          }
          _UserModel.updateUsrMessageStat(params, function (data) {
            _self.setData({
              show:false
            })
          })
        })

  },
  pass:function(e){
      let target = e.target.dataset.current;
      let message= _self.data.messages[target];
      _self.setData({
         currentMsg:message,
         currentTarget:target
      })
      if (message.messageType =='MESSAGE_ADD_FRIEND'){
        let uid = message.messageSource;
      
        _UserModel.handleFriendRequest(uid,function(data){
          let messageId = message.messageId;
          let messageArr = new Array(messageId + "");
          //   0-未读 1-已读 2-忽略 3-已处理
          let params = {
            "messageIdList": messageArr,
            "messageStatus": "3"
          }
          _UserModel.updateUsrMessageStat(params, function (data) {
            wx.showToast({
              title: '已通过',
              icon: 'success',
              duration: 2000
            })
          });
         
        })
      }

      if (message.messageType == 'MESSAGE_REAL_NAME_VERIFY') {
        Dialog.confirm({
          message: message.messageContent,
          confirmButtonText:"通过",
          cancelButtonText:"拒绝"
        }).then(() => {
                let uid = message.messageSource;
                let params = {
                  "handleUserId": uid,
                  "ischecked": "02" //02通过
                } 
               _UserModel.handleAuthentication(params, function (data) {

                  let messageId = message.messageId;
                  let messageArr = new Array(messageId + "");
                  let params = {
                    "messageIdList": messageArr,
                    "messageStatus": "3"
                  }
                  _UserModel.updateUsrMessageStat(params, function (data) {
                    wx.showToast({
                      title: '已通过',
                      icon: 'success',
                      duration: 2000
                    })
                  })
                })
        }).catch(() => {
          _self.setData({
            show:true
          })
        });
      }
  },
  ignore:function(e){
    // 0-未读 1-已读 2-忽略 3-已处理
    let target = e.target.dataset.current;
    let message = _self.data.messages[target];
    let messageId = message.messageId;
    let messageArr = new Array(messageId+"");
    let params={
      "messageIdList": messageArr,
      "messageStatus": "2"
    }
    _UserModel.updateUsrMessageStat(params,function(data){
      message.messageStatus=2;
      var tar = 'messages['+target+'].messageStatus'
      _self.setData({
        [tar]:2
      })
    })
  },
  onReachBottom() {
    let pagenation = _self.data.pagenation;
    if (pagenation.hasMore) {
      let newPageNum = pagenation.pageNum + 1;
      let pages = pagenation.pages;
      if (newPageNum > pages) {
        _self.setData({
          ['pagenation.hasMore']: false,
          isNoMoreData: true
        })
      } else {
        _self.setData({
          ['pagenation.pageNum']: newPageNum
        })
        _self.getUserMessage(_self.data.pagenation);
      }

    } else {
      _self.setData({
        isNoMoreData: true
      })
    }
  }
})